/**
 * 菜单配置
 */
layui.define([ 'element', "fsConfig", "fsCommon" ], function(exports) {

	var menuConfig = {
		dataType : "local", //获取数据方式，local本地获取，server 服务端获取
		loadUrl : "", //加载数据地址
		method : "post", //请求类型，默认post
		rootMenuId : "0", //根目录菜单id
		defaultSelectTopMenuId : "1", //默认选中头部菜单id
		defaultSelectLeftMenuId : "", //默认选中左边菜单id
		menuIdField : "menuId", //菜单id
		menuNameField : "menuName", //菜单名称
		menuIconField : "menuIcon", //菜单图标，图标必须用css
		menuHrefField : "menuHref", //菜单链接
		parentMenuIdField : "parentMenuId", //父菜单id
		data : [
			{
				"menuId" : "1",
				"menuName" : "首页",
				"menuIcon" : "",
				"menuHref" : "home",
				"parentMenuId" : "0"
			}, {
				"menuId" : "10000",
				"menuName" : "消息管理",
				"menuIcon" : "&#xe620;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "10001",
				"menuName" : "微信管理",
				"menuIcon" : "&#xe63a;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "10002",
				"menuName" : "短信管理",
				"menuIcon" : "&#xe63b;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "10003",
				"menuName" : "邮件管理",
				"menuIcon" : "&#xe609;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "10004",
				"menuName" : "站内信管理",
				"menuIcon" : "&#xe613;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "10005",
				"menuName" : "公告管理",
				"menuIcon" : "&#xe62b;",
				"menuHref" : "",
				"parentMenuId" : "1"
			}, {
				"menuId" : "20001",
				"menuName" : "微信配置",
				"menuIcon" : "&#xe614;",
				"menuHref" : "wx/conf/view",
				"parentMenuId" : "10001"
			}, {
				"menuId" : "20002",
				"menuName" : "公众号模版",
				"menuIcon" : "&#xe630;",
				"menuHref" : "wx/temp/view?type=0",
				"parentMenuId" : "10001"
			}, {
				"menuId" : "20003",
				"menuName" : "小程序模版",
				"menuIcon" : "&#xe630;",
				"menuHref" : "wx/temp/view?type=1",
				"parentMenuId" : "10001"
			}, {
				"menuId" : "20004",
				"menuName" : "微信消息日志",
				"menuIcon" : "&#xe642;",
				"menuHref" : "wx/logs/view",
				"parentMenuId" : "10001"
			}, {
				"menuId" : "20005",
				"menuName" : "短信配置",
				"menuIcon" : "&#xe614;",
				"menuHref" : "sms/conf/view",
				"parentMenuId" : "10002"
			}, {
				"menuId" : "20006",
				"menuName" : "短信模版",
				"menuIcon" : "&#xe630;",
				"menuHref" : "sms/temp/view",
				"parentMenuId" : "10002"
			}, {
				"menuId" : "20007",
				"menuName" : "短信日志",
				"menuIcon" : "&#xe642;",
				"menuHref" : "sms/logs/view",
				"parentMenuId" : "10002"
			}, {
				"menuId" : "20008",
				"menuName" : "邮件配置",
				"menuIcon" : "&#xe614;",
				"menuHref" : "mail/conf/view",
				"parentMenuId" : "10003"
			}, {
				"menuId" : "20009",
				"menuName" : "邮件模版",
				"menuIcon" : "&#xe630;",
				"menuHref" : "mail/temp/view",
				"parentMenuId" : "10003"
			}, {
				"menuId" : "20010",
				"menuName" : "邮件日志",
				"menuIcon" : "&#xe642;",
				"menuHref" : "mail/logs/view",
				"parentMenuId" : "10003"
			}
		]
	};

	var element = layui.element,
		fsCommon = layui.fsCommon,
		fsConfig = layui.fsConfig,
		statusName = $.result(fsConfig, "global.result.statusName", "code"),
		msgName = $.result(fsConfig, "global.result.msgName", "message"),
		successNo = $.result(fsConfig, "global.result.successNo", 1000),
		dataName = $.result(fsConfig, "global.result.dataName", "entity"),
		FsMenu = function() {};


	FsMenu.prototype.render = function() {
		this.loadData();
		this.showMenu();
	};

	/**
	 * 加载数据
	 */
	FsMenu.prototype.loadData = function() {

		if (menuConfig.dataType == "server") { //服务端拉取数据

			var url = menuConfig.loadUrl;
			if ($.isEmpty(url)) {
				fsCommon.errorMsg("未配置请求地址！");
				return;
			}

			fsCommon.invoke(url, {}, function(data) {
				if (data[statusName] == successNo) {
					menuConfig.data = $.result(data, dataName);
				} else {
					//提示错误消息
					fsCommon.errorMsg(data[msgName]);
				}
			}, false, menuConfig.method);

		}

	}


	/**
	 * 获取图标
	 */
	FsMenu.prototype.getIcon = function(menuIcon) {

		if (!$.isEmpty(menuIcon)) {

			if (menuIcon.indexOf("<i") == 0) {
				return menuIcon;
			} else if (menuIcon.indexOf("&#") == 0) {
				return '<i class="layui-icon">' + menuIcon + '</i>';
			} else if (menuIcon.indexOf("fa-") == 0) {
				return '<i class="fa ' + menuIcon + '"></i>';
			} else {
				return '<i class="' + menuIcon + '"></i>';
			}
		}
		return "";
	};

	/**
	 * 清空菜单
	 */
	FsMenu.prototype.cleanMenu = function() {
		$("#fsTopMenu").html("");
		$("#fsLeftMenu").html("");
	}
	
	/**
	 * 显示菜单
	 */
	FsMenu.prototype.showMenu = function() {
		var thisMenu = this;
		var data = menuConfig.data;
		if (!$.isEmpty(data)) {
			var _index = 0;
			//显示顶部一级菜单
			var fsTopMenuElem = $("#fsTopMenu");
			var fsLeftMenu = $("#fsLeftMenu");
			$.each(data, function(i, v) {
				if (menuConfig.rootMenuId === v[menuConfig.parentMenuIdField]) {

					var topStr = '<li class="layui-nav-item';
					if ($.isEmpty(menuConfig.defaultSelectTopMenuId) && _index === 0) { //为空默认选中第一个
						topStr += ' layui-this';
					} else if (!$.isEmpty(menuConfig.defaultSelectTopMenuId) && menuConfig.defaultSelectTopMenuId == v[menuConfig.menuIdField]) { //默认选中处理
						topStr += ' layui-this';
					}
					_index++ ;
					topStr += '" dataPid="' + v[menuConfig.menuIdField] + '"><a href="javascript:;">' + thisMenu.getIcon(v[menuConfig.menuIconField]) + ' <cite>' + v[menuConfig.menuNameField] + '</cite></a></li>';
					fsTopMenuElem.append(topStr);

					//显示二级菜单，循环判断是否有子栏目
					$.each(data, function(i2, v2) {
						if (v[menuConfig.menuIdField] === v2[menuConfig.parentMenuIdField]) {

							var menuRow = '<li class="layui-nav-item';
							if (!$.isEmpty(menuConfig.defaultSelectLeftMenuId) && menuConfig.defaultSelectLeftMenuId == v2[menuConfig.menuIdField]) { //默认选中处理
								menuRow += ' layui-this';
							}
							//显示三级菜单，循环判断是否有子栏目
							var menuRow3 = "";
							$.each(data, function(i3, v3) {
								if (v2[menuConfig.menuIdField] === v3[menuConfig.parentMenuIdField]) {
									if ($.isEmpty(menuRow3)) {
										menuRow3 = '<dl class="layui-nav-child">';
									}
									menuRow3 += '<dd';
									if (!$.isEmpty(menuConfig.defaultSelectLeftMenuId) && menuConfig.defaultSelectLeftMenuId == v3[menuConfig.menuIdField]) { //默认选中处理
										menuRow3 += ' class="layui-this"';
										menuRow += ' layui-nav-itemed'; //默认展开二级菜单
									}

									menuRow3 += ' lay-id="' + v3[menuConfig.menuIdField] + '"><a href="javascript:;" menuId="' + v3[menuConfig.menuIdField] + '" dataUrl="' + v3[menuConfig.menuHrefField] + '">' + thisMenu.getIcon(v3[menuConfig.menuIconField]) + ' <cite>' + v3[menuConfig.menuNameField] + '</cite></a></dd>';

								}

							});

							menuRow += '" lay-id="' + v2[menuConfig.menuIdField] + '" dataPid="' + v2[menuConfig.parentMenuIdField] + '" style="display: none;"><a href="javascript:;" menuId="' + v2[menuConfig.menuIdField] + '" dataUrl="' + v2[menuConfig.menuHrefField] + '">' + thisMenu.getIcon(v2[menuConfig.menuIconField]) + ' <cite>' + v2[menuConfig.menuNameField] + '</cite></a>';

							if (!$.isEmpty(menuRow3)) {
								menuRow3 += '</dl>';

								menuRow += menuRow3;
							}

							menuRow += '</li>';

							fsLeftMenu.append(menuRow);
						}

					});

				}
			});
		}
		element.render("nav");
	};

	var fsMenu = new FsMenu();
	exports("fsMenu", fsMenu);
});